package com.cskaoyan.catmall.service.Impl;

import com.cskaoyan.catmall.dao.MarketCollectMapper;
import com.cskaoyan.catmall.model.bo.BaseParamBo;
import com.cskaoyan.catmall.model.vo.CollectDataVO;
import com.cskaoyan.catmall.model.vo.CollectListVO;
import com.cskaoyan.catmall.service.CollectService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @date: 2022-01-08-10-16
 * @author: ZYY
 * @description: 收藏服务的实现类
 */
@Service
public class CollectServiceImp implements CollectService {
    @Autowired
    MarketCollectMapper collectMapper;

    @Override
    public CollectDataVO list(BaseParamBo params, Integer userId, Integer valueId) {
        Integer limit = params.getLimit(); //数据量
        Integer page = params.getPage(); // 页码
        String order = params.getOrder(); //排序列
        String sort = params.getSort(); // 排序规则
        // 使用PageHelper
        // 开启分页功能，传入页码和数据量
        PageHelper.startPage(page, limit);

        // 查询数据库
        List<CollectListVO> list = collectMapper.selectInfoSelective(userId, valueId, order, sort);

        // 将获得的结果封装到pageInfo中
        PageInfo<CollectListVO> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();

        CollectDataVO addressDataVO = CollectDataVO.data(total,pages,limit,page,list);
        return addressDataVO;
    }
}